﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Odbc;

namespace MyLibrary.Logger
{
    /// <summary>
    /// LogWriter集合
    /// </summary>
    /// <typeparam name="Writer">LogWriter</typeparam>
    public class LogWriterCollection<Writer> : System.Collections.CollectionBase where Writer : ILogWriter
    {
        /// <summary>
        /// 索引器[按名称索引][只读]
        /// </summary>
        /// <param name="name">Name of ILogWriter</param>
        /// <returns>ILogWriter</returns>
        public Writer this[string name]
        {
            get
            {
                if (String.IsNullOrEmpty(name)) throw new NullReferenceException("索引名称不能为空");
                foreach (Writer writer in this.List)
                {
                    if ((ILogWriter)writer.LogWriterName == name)
                        return writer;
                }
                throw new KeyNotFoundException("无法找到对应的项");
            }
        }

        /// <summary>
        /// 索引器[按序号索引][只读]
        /// </summary>
        /// <param name="index">序号</param>
        /// <returns>ILogWriter</returns>
        public Writer this[int index]
        {
            get 
            {
                if (index >= this.List.Count) throw new OverflowException("索引超出界限");
                return this.List[index]; 
            }
        }
    }

    /// <summary>
    /// 将日志写入数据库
    /// </summary>
    public class ADOLogWriter : LogWriterBase, ILogWriter
    {
        public event ILogWriter.LogWriteEventHandler OnLogWrite;
        private string _connectionString = String.Empty;
        private string _insertCommand = String.Empty;
        private string _writerName = String.Empty;
        private MessageFormat _messageFormat = MessageFormat.Simple;
        OdbcConnection connection = null;

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public ADOLogWriter()
        {
            connection = new OdbcConnection();
        }
        /// <summary>
        /// 析构函数
        /// </summary>
        ~ADOLogWriter()
        {
            dispose(false);
        }
        #endregion

        /// <summary>
        /// 记录写入器名称
        /// </summary>
        public string LogWriterName
        {
            get { return _writerName; }
            set { _writerName = value; }
        }

        /// <summary>
        /// 写入日志数据
        /// </summary>
        public void DoLogWriter(LoggingEventData data)
        {
            //判断日志的消息是否为空
            if (String.IsNullOrEmpty(data.Message)) throw new ArgumentException("日志消息不能为空");
            //组合写入数据库的记录
        }

        /// <summary>
        /// 关闭日志写入器,释放所有资源
        /// </summary>
        public void CloseWriter()
        {
            
        }

        /// <summary>
        /// 重写基类的购置函数
        /// </summary>
        /// <param name="configs"></param>
        protected override void  InitLogWriter(Dictionary<string,string> configs)
        {
            //载入特殊的配置
            if (!configs.ContainsKey("ConnectionString")) throw new ArgumentException("配置集合中不存在数据库连接字符串");
            configs.ContainsKey("CommandText") == false ? _insertCommand = "Insert Into MyLog([Date],[Thread],[Logger],[Message]) Value (@date,@thread,@logger,@message)" : _insertCommand = configs["CommandText"];
            connection.ConnectionString = configs["ConnectionString"];
            //载入通用的配置
            base.InitLogWriter(this, configs);
        }


        #region IDisposable Members
        
        private void dispose(bool disposing)
        {
            if (disposing)
            {
                if (connection != null)
                {
                    if (connection.State != ConnectionState.Closed)
                        connection.Close();
                    connection.Dispose();
                    connection = null;
                }
            }
        }
        /// <summary>
        /// 释放类占用的资源
        /// </summary>
        public void Dispose()
        {
            dispose(true);
        }

        #endregion

        #region ILogWriter Evernt

        event ILogWriter.LogWriteEventHandler ILogWriter.OnLogWrite
        {
            add { throw new NotImplementedException(); }
            remove { throw new NotImplementedException(); }
        }

        #endregion
    }

    /// <summary>
    /// 将日志写入.Log文件中
    /// </summary>
    public class FileLogWriter : LogWriterBase, ILogWriter
    {
        public FileLogWriter()
        {
            throw new System.NotImplementedException();
        }

        ~FileLogWriter()
        {
            throw new System.NotImplementedException();
        }

        #region ILogWriter Members

        public string LogWriterName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public void DoLogWriter(LoggingEvent loggingEvent, bool sendAlarm)
        {
            throw new NotImplementedException();
        }

        public void CloseWriter()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region ILogWriter Members

        public event ILogWriter.LogWriteEventHandler OnLogWrite;

        #endregion

        #region ILogWriter Members

        event ILogWriter.LogWriteEventHandler ILogWriter.OnLogWrite
        {
            add { throw new NotImplementedException(); }
            remove { throw new NotImplementedException(); }
        }

        public Dictionary<string, string> ConfigCollection
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion
    }

    /// <summary>
    /// 将日志输出到控制台
    /// </summary>
    public class ConsoleLogWriter : LogWriterBase, ILogWriter
    {
        public ConsoleLogWriter()
        {
            throw new System.NotImplementedException();
        }

        ~ConsoleLogWriter()
        {
            throw new System.NotImplementedException();
        }
        #region ILogWriter Members

        public string LogWriterName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public void DoLogWriter(LoggingEvent loggingEvent, bool sendAlarm)
        {
            throw new NotImplementedException();
        }

        public void CloseWriter()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region ILogWriter Members

        public event ILogWriter.LogWriteEventHandler OnLogWrite;

        #endregion

        #region ILogWriter Members

        event ILogWriter.LogWriteEventHandler ILogWriter.OnLogWrite
        {
            add { throw new NotImplementedException(); }
            remove { throw new NotImplementedException(); }
        }

        public Dictionary<string, string> ConfigCollection
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion
    }

    /// <summary>
    /// 将日志以手机短信的方式发送
    /// </summary>
    public class MobileLogWriter : LogWriterBase, ILogWriter
    {
        public MobileLogWriter()
        {
            throw new System.NotImplementedException();
        }

        ~MobileLogWriter()
        {
            throw new System.NotImplementedException();
        }
        #region ILogWriter Members

        public string LogWriterName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public void DoLogWriter(LoggingEvent loggingEvent, bool sendAlarm)
        {
            throw new NotImplementedException();
        }

        public void CloseWriter()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region ILogWriter Members

        public event ILogWriter.LogWriteEventHandler OnLogWrite;

        #endregion

        #region ILogWriter Members

        event ILogWriter.LogWriteEventHandler ILogWriter.OnLogWrite
        {
            add { throw new NotImplementedException(); }
            remove { throw new NotImplementedException(); }
        }

        public Dictionary<string, string> ConfigCollection
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion
    }

    /// <summary>
    /// 将日志以XML的形式进行保存
    /// </summary>
    public class XMLLogWriter : LogWriterBase, ILogWriter
    {
        public XMLLogWriter()
        {
            throw new System.NotImplementedException();
        }

        ~XMLLogWriter()
        {
            throw new System.NotImplementedException();
        }

        #region ILogWriter Members

        public string LogWriterName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public void DoLogWriter(LoggingEvent loggingEvent, bool sendAlarm)
        {
            throw new NotImplementedException();
        }

        public void CloseWriter()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region ILogWriter Members

        public event ILogWriter.LogWriteEventHandler OnLogWrite;

        #endregion

        #region ILogWriter Members

        event ILogWriter.LogWriteEventHandler ILogWriter.OnLogWrite
        {
            add { throw new NotImplementedException(); }
            remove { throw new NotImplementedException(); }
        }

        #endregion

        #region ILogWriter Members

        event ILogWriter.LogWriteEventHandler ILogWriter.OnLogWrite
        {
            add { throw new NotImplementedException(); }
            remove { throw new NotImplementedException(); }
        }

        #endregion

        #region ILogWriter Members

        event ILogWriter.LogWriteEventHandler ILogWriter.OnLogWrite
        {
            add { throw new NotImplementedException(); }
            remove { throw new NotImplementedException(); }
        }

        #endregion

        #region ILogWriter Members

        event ILogWriter.LogWriteEventHandler ILogWriter.OnLogWrite
        {
            add { throw new NotImplementedException(); }
            remove { throw new NotImplementedException(); }
        }

        public Dictionary<string, string> ConfigCollection
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion
    }
}
